<?php
namespace Admin\Controller;
use Tools\AdminController;
class AuthController extends AdminController {
    	
		
    public function showlist(){
//  	//查找全部权限
//  	$auth_info=D('Auth')->select();
		$auth=new \Model\AuthModel();
		//分页相关
		$total=$auth->count();
		$per=6;
		//实例化分页类对象
		$page_obj=new \Tools\CssPage($total,$per);
		$sql="select * from auth order by id asc ".$page_obj->limit;
		$auth_info=$auth->query($sql);
		$pagelist=$page_obj->fpage(array(3,4,5,6,7,8));
		$this->assign('total',$total);
		$this->assign('pagelist',$pagelist);
		$this->assign('auth_info',$auth_info);
    	$this->display();
    }
	
	public function updateauth($id){
		if(!empty($_POST)){
		//var_dump($_POST);
		$auth=new \Model\AuthModel();
		//var_dump($_POST);
		if($auth->save($_POST)){
			$this->success('Index/success');
			$this->redirect('Auth/showlist',array(),3," ");
		}else{
			$this->error('Index/error');
			$this->redirect('Auth/showlist',array(),3," ");
		}
		}else{
		$auth_info=D('Auth')->field("id,name,controller,action,level")->find($id);
		$this->assign("auth_info",$auth_info);
		$this->display();
		}
	}
	function addauth(){
		if(!empty($_POST)){
			//判断权限等级并生成全路径path
			$_POST['path']=($_POST['level']=="1")?$_POST['pid']."-".$_POST['id']:$_POST['id'];
			//var_dump($_POST);
			if(D('Auth')->add($_POST)){
				$this->success('Index/success');//添加成功
				$this->redirect('Auth/showlist',array(),3," ");
			}else{
				$this->error('Index/error');//添加失败
				$this->redirect('Auth/showlist',array(),3," ");
			}
		}else{
		$authp=D('Auth')->where("level=0")->select();
		$this->assign('authp',$authp);
		$this->display();
		}
	}
	function deleteauth($id){
		echo $id;
		if(D('Auth')->delete($id)){
				$this->success('Index/success');//添加成功
				$this->redirect('Auth/showlist',array(),3," ");
			}else{
				$this->error('Index/error');//添加失败
				$this->redirect('Auth/showlist',array(),3," ");
			}
	}
}